﻿#include "cpublicdataex.h"
#include "cinihelp.h"
#include <QCoreApplication>
#include "CLogOper.h"
#include "headerex.h"
#include <QMessageBox>

int messageBox(int nMsgLevel, QString sMsg)
{
    if (nMsgLevel == 0)
    {
        int nRet=QMessageBox::information(nullptr, "信息", sMsg);
        return nRet;
    }
    else if (nMsgLevel == 1)
    {
        int nRet=QMessageBox::warning(nullptr, "警告", sMsg);
        return nRet;
    }
    else if (nMsgLevel == 2)
    {
        int nRet = QMessageBox::critical(nullptr, "错误", sMsg);
        return nRet;
    }
    else if (nMsgLevel == 3)
    {
        int nRet = QMessageBox::question(nullptr, "询问", sMsg, QMessageBox::Yes | QMessageBox::No);
        return nRet;
    }

    return 0;
}


CPublicDataEx::CPublicDataEx()
{
    m_nDbType = 0;
    m_sConnectInfo = "";
	
    getIniConfigData();
}

CPublicDataEx::~CPublicDataEx()
{
	m_cDataBaseOperMgr.stopWorkThread();
}

//初始化数据库
bool CPublicDataEx::initSql()
{
    if(m_sConnectInfo.size() < 1)
    {
        SExecLog logInfo;
        logInfo.m_nAlarmLevel = enLogTypeError;
        logInfo.m_sObject = QObject::tr("数据库");
        logInfo.m_sLogDesc = QObject::tr("数据库连接字符串为空");
        CLogOper::instance().addLogMsg("MainWindow", logInfo);

        return false;
    }

    QStringList sList = m_sConnectInfo.split(";");
    if (sList.size() >= 5)
    {
        CDataBaseConfig cCfg;
        switch (m_nDbType) {
        case 0:
            cCfg.sDBType = "QMYSQL";
            break;
        case 1:
            //cCfg.sDBType = "QOCI";
            cCfg.sDBType = "Oracle";
            break;
        case 2:
            cCfg.sDBType = "QODBC";
            break;
        default:
            cCfg.sDBType = "QMYSQL";
            break;
        }

        {
            SExecLog logInfo;
            logInfo.m_nAlarmLevel = enLogTypeRun;
            logInfo.m_sObject = QObject::tr("数据库");
            logInfo.m_sLogDesc = QObject::tr("当前加载DBType:%1").arg(cCfg.sDBType);
            CLogOper::instance().addLogMsg("MainWindow", logInfo);
        }


        cCfg.sConnectName = "";
        cCfg.sIP = sList[2].trimmed();
        cCfg.nPort = sList[1].trimmed().toInt();
        cCfg.sName = sList[0].trimmed();
        cCfg.sUserName = sList[3].trimmed();
        cCfg.sPassWord = sList[4].trimmed();

        qDebug() << cCfg.sIP << cCfg.nPort << cCfg.sName << cCfg.sUserName << cCfg.sUserName << cCfg.sPassWord;

        QVector<QString> vecTabelName;//表名vec
        QStringList sList2 = g_sTableName.split("-");
        if (sList2.size() >= 1)
        {
            for (int i = 0; i < sList2.size(); i++)
            {
                if (sList2[i].size() > 1)
                {
                    vecTabelName.push_back(sList2[i]);
                }
            }
        }


        //设置读写连接数
        m_cDataBaseOperMgr.setConnectNumber(5, 5);
        //设置连接信息及数据库表
        m_cDataBaseOperMgr.setDbConfigInfo(cCfg, vecTabelName);
        //m_cDataBaseOperMgr.startWorkThread(0);
        m_cDataBaseOperMgr.startWorkThread(0);

        return true;

    }
    else
    {
        SExecLog logInfo;
        logInfo.m_nAlarmLevel = enLogTypeError;
        logInfo.m_sObject = QObject::tr("数据库");
        logInfo.m_sLogDesc = QObject::tr("数据库连接字符串格式配置错误");
        CLogOper::instance().addLogMsg("MainWindow", logInfo);

        return false;

    }

    return false;
}



//获取ini配置文件信息
bool CPublicDataEx::getIniConfigData()
{
    QString sExePath = QCoreApplication::applicationDirPath();

    QString sIniPath = sExePath + "/PoundListPrint.ini";
    CIniHelp iniHelp;
    iniHelp.settingsInit(sIniPath);

    m_sLedIp = iniHelp.getValue("Led", "LedIp");
    m_nLedPicRefSec = iniHelp.getValue("Led", "LedPicRefSec").toInt();

    m_nDbType = iniHelp.getValue("DataBase", "DbType").toInt();//数据库类型(0:MySql 1:Oracle 2:SqlServer)
    m_sConnectInfo = iniHelp.getValue("DataBase", "DbConnectStr");//数据库连接字符串
    qDebug() << "nDbType:" << m_nDbType << " DbConnectStr:" << m_sConnectInfo << "m_sLedIp:" << m_sLedIp << " m_nLedPicRefSec:" << m_nLedPicRefSec;

    {
        SExecLog logInfo;
        logInfo.m_nAlarmLevel = enLogTypeRun;
        logInfo.m_sObject = "系统";
        logInfo.m_sLogDesc = "ini配置文件读取完成";
        CLogOper::instance().addLogMsg("MainWindow", logInfo);
    }

    return true;
}
